package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-28 18:18 星期六
 * description:
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierDao supplierDao;

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.getSuplierList(offSet,rows,supplierName);

        map.put("rows",supplierList);
        map.put("total",null);
        return map;
    }

    /**
     * 保存或修改供应商
     * @param supplier
     * @return
     */
    @Override
    public ServiceVO saveOrUpdate(Supplier supplier) {
        Integer supplierId = supplier.getSupplierId();
        if (supplierId != null){
            //此次为修改操作
            //如果前端传递的值不为null就修改
            supplierDao.update(supplier);
        }else{
            //此次为添加操作
            supplierDao.save(supplier);
        }
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @Override
    public ServiceVO delete(String ids) {

        String[] idsArray = ids.split(",");
        ArrayList<Integer> idsList = new ArrayList<>();
        for (String id : idsArray) {
            idsList.add(Integer.parseInt(id));
        }

        supplierDao.delete(idsList);

        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 供应商查询
     * @param q
     * @return
     */
    @Override
    public List<Supplier> getComboboxList(String q) {

        List<Supplier> supplierList = supplierDao.getComboboxList(q);
        return supplierList;
    }
}
